package queues;

import helpers.CrawlerHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.AutoSiteModel;
import models.NewsModel;


import data.AutoSiteData;
import data.NewsData;
import debugs.AutoSiteDebug;
import debugs.NewsDebug;

public class QueueCrawlerNews extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(QueueCrawlerNews.class.getName());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		log.setLevel(Level.FINE);
		String queueId = (String) URLDecoder.decode(request.getParameter("queueid"));
		String siteUrl = (String) URLDecoder.decode(request.getParameter("siteUrl"));
		if (queueId != null && siteUrl != null) {
			AutoSiteData aData = new AutoSiteData();
			NewsData newsData = new NewsData();
			NewsModel demoNews = new NewsModel();
			if (!newsData.checkNewsExists(queueId, siteUrl)) {
				AutoSiteModel autoSite = aData.getSite(siteUrl);
				NewsModel news = CrawlerHelper.getNews(queueId, autoSite);
				newsData.insert(news);
				NewsDebug.debug(news);
				response.setContentType("text/plain");
				NewsDebug.debugWeb(news, response);
				response.getWriter().println("Update success");
			}
		}
		
	}
}
